<template>
  <div class="container">
    <el-card>
      <div slot="header" class="clearfix" v-if="$route.query.subjectName">
        <span style="font-size: 14px;color: #606266;" >学科管理  <i class="el-icon-arrow-right"></i>  {{ $route.query.subjectName }}  <i class="el-icon-arrow-right"></i>  目录管理</span>
      </div>
      <el-row>
        <el-form ref="form" :model="page" label-width="80px">
          <el-col :span="5.5">
            <el-form-item size="small" label="目录名称">
              <el-input v-model="page.directoryName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="margin-left: 10px">
            <el-form-item size="small" label="状态">
              <el-select v-model="page.state" placeholder="请选择">
                <el-option label="启用" :value="1"></el-option>
                <el-option label="禁用" :value="0"></el-option>
              </el-select>
              <el-button style="margin-left: 10px" size="small" @click="clerFn"
                >清除</el-button
              >
              <el-button type="primary" size="small" @click="hGetList"
                >搜索</el-button
              >
            </el-form-item>
          </el-col>
          <el-col :span="7" style="margin-left: 60px; text-align: end">
            <el-link v-if="$route.query.subjectName" @click="$router.back()" type="primary" icon="el-icon-back" style="margin-right: 10px;" >返回学科</el-link>
            <el-button
              @click="onNewly"
              type="success"
              size="small"
              icon="el-icon-edit"
              >新增目录</el-button
            >
          </el-col>
        </el-form>
      </el-row>
      <TotalityAdd :consts="counts" />
      <LabelCatalog :table-data="tableData" @h-getList="hGetList">
        <template #name1>
          <el-table-column prop="totals" label="面试题数量"> </el-table-column>
        </template>
      </LabelCatalog>
      <div class="block" v-if="!addOpde">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          background
          :page-sizes="[5, 10, 20, 30]"
          :page-size="10"
          :total="counts"
          layout="prev, pager, next,sizes, jumper"
        >
        </el-pagination>
      </div>
    </el-card>
    <DirectorysAdd
      :dialog-visible="dialogVisible"
      :select-data="selectData"
      @handle-close="handleClose"
    />
  </div>
</template>

<script>
import { list } from '@/api/hmmm/directorys'
import LabelCatalog from '../components/LabelCatalog'
import DirectorysAdd from '../components/directorys-add'
import { simple } from '@/api/hmmm/subjects'
export default {
  components: {
    LabelCatalog,
    DirectorysAdd
  },
  data () {
    return {
      counts: null,
      tableData: [],
      page: {
        page: 1,
        pagesize: 10,
        directoryName: '',
        state: null,
        subjectID: this.$route.query.id
      },
      currentPage4: 1,
      addOpde: false,
      dialogVisible: false,
      selectData: []
    }
  },
  mounted () {
    this.hGetList()
    // this.page.id = this.$route.query.id
  },
  methods: {
    handleClose () {
      this.dialogVisible = false
      this.hGetList()
    },
    async onNewly () {
      const res = await simple()
      this.selectData = res.data
      this.dialogVisible = true
    },
    clerFn () {
      this.page = {
        page: 1,
        pagesize: 10,
        directoryName: '',
        state: null
      }
    },
    async hGetList () {
      console.log()
      const newArr = {}
      for (const key in this.page) {
        if (this.page[key]) {
          newArr[key] = this.page[key]
        }
      }
      const res = await list(newArr)
      this.tableData = res.data.items
      this.counts = res.data.counts
      this.addOpde = this.page.directoryName || this.page.state
    },
    // 控制条数
    handleSizeChange (val) {
      this.page.pagesize = val
      this.hGetList()
    },
    // 控制页数
    handleCurrentChange (val) {
      this.page.page = val
      this.hGetList()
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding: 10px;

  .block {
    margin-top: 20px;
    text-align: right;
    ::v-deep .el-pagination__jump {
      margin: 0;
    }
  }
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;

}
</style>
